﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using XNOgre.Core;

namespace XNOgre.Graphics
{
    /// <summary>
    /// Contains a Viewport with additional information for rendering
    /// </summary>
    public class ViewportTarget
    {
        private int zOrder;
        private Color color;
        private Viewport vp;

        public event EventHandler ZOrderChanged;

        internal ViewportTarget(Viewport vp, int zOrder = 0)
            : this(vp, Color.Black, zOrder)
        { }
        internal ViewportTarget(Viewport vp, Color color, int zOrder = 0)
        {
            this.vp = vp;
            this.color = color;
            this.zOrder = zOrder;
            ShadowsEnabled = true;
            ShowOverlays = true;
        }
        /// <summary>
        /// Gets/Sets the RenderTarget
        /// If left null, the GraphicsDevice will render to the backbuffer (default)
        /// If not null, the given RenderTarget will receive the frame(s).
        /// <remarks>Used in RTT (Render to texture)</remarks>
        /// </summary>
        public RenderTarget2D RenderTarget { get; set; }
        /// <summary>
        /// Height of viewport in pixels
        /// </summary>
        public int ActualHeight
        {
            get { return vp.Height; }
        }
        /// <summary>
        /// Height of viewport in pixels
        /// </summary>
        public int ActualWidth
        {
            get { return vp.Width; }
        }
        /// <summary>
        /// Returns X position of viewport in pixels
        /// </summary>
        public float ActualLeft
        {
            get { return vp.X; }
        }
        /// <summary>
        /// Returns Y position of viewport in pixels
        /// </summary>
        public float ActualTop
        {
            get { return vp.Y; }
        }
        /// <summary>
        /// Height of the viewport in a range of 0-1f
        /// </summary>
        public float RelativeHeight
        {
            get;
            internal set;
        }
        /// <summary>
        /// Width of the viewport in a range of 0-1f
        /// </summary>
        public float RelativeWidth
        {
            get;
            internal set;
        }
        /// <summary>
        /// X value of viewport, in a range of 0-1f
        /// </summary>
        public float RelativeLeft
        {
            get;
            internal set;
        }
        /// <summary>
        /// Y value of viewport, in a range of 0-1f
        /// </summary>
        public float RelativeTop
        {
            get;
            internal set;
        }
        public Viewport Viewport
        {
            get { return vp; }
            set { vp = value; }
        }
        public bool ShadowsEnabled { get; set; }
        public bool ShowOverlays { get; set; }
        public Color Color
        {
            get { return color; }
            set { color = value; }
        }
        public int ZOrder
        {
            get { return zOrder; }
            set
            {
                bool different = (zOrder != value);

                zOrder = value;
                if (different)
                {
                    if (ZOrderChanged != null)
                    {
                        ZOrderChanged(this, EventArgs.Empty);
                    }
                }
            }
        }
    }
}
